package com.ttl.bc.ic.stktransfer;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;


import com.ttl.bc.ic.stktransfer2.BCStkTransfer2BO;
import com.ttl.bc.ic.stktransfer2.BCStkTransfer2Form;
import com.ttl.common.util.CommonUtils;
import com.ttl.common.util.Page;

public class BCStkTransferBO {
	
	public void showFind(HttpServletRequest request) throws UnsupportedEncodingException {

		String	itemFind = "";
		if(request.getParameter("itemFind")!=null) {
			itemFind = ((String)request.getParameter("itemFind"));
			itemFind = new String(itemFind.getBytes("ISO8859_1"),"UTF-8");
			request.setAttribute("itemFind", itemFind.trim());
		}		
		
		String dept = request.getParameter("dept").toString();
		String hql = "";
		if(itemFind==null||itemFind.trim().equals("")) {
			hql = "FROM BCStkTransfer2 " +
					 "WHERE docNo like '%"+dept.trim()+"%' " +
					 "ORDER BY docNo desc";			
		}else {
			hql = "FROM BCStkTransfer2  " +
					 "WHERE docNo like '%"+dept.trim()+"%' " +
					 "and docNo like '%"+itemFind.trim()+"%' "+					 
					 "ORDER BY docNo desc";			
		}
		System.out.println(hql);
		Page.setPage(request, hql);
	}

	public boolean approve(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		
		return new BCStkTransfer2BO().view(form, request, response);
	}

	public boolean add(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		

		BCStkTransfer s = setBean(form, request);
		
		ArrayList<BCStkTransfSub3> s3 = setSub3(s);
		
		String dept = request.getParameter("dept").toString();
//		request.setAttribute("message", "แก้ไขข้อมูลใบขอโอนเรียบร้อย");
		request.setAttribute("dept", dept);
		request.setAttribute("pageForward", "<meta http-equiv='refresh' content='1;" +
				"url=stktransfer.do?mode=showFind&dept="+dept+"&docNo="+s.getDocNo()+"'>");	
		request.setAttribute("itemFind", s.getDocNo());
		
		request.setAttribute("message", "บันทึกข้อมูลใบโอนเรียบร้อย: "+s.getDocNo());
		return 	new BCStkTransferDAO().add(s, s3);
	}
	
	@SuppressWarnings("unchecked")
	private ArrayList<BCStkTransfSub3> setSub3(BCStkTransfer s) {
		
		ArrayList<BCStkTransfSub3> al = new ArrayList<BCStkTransfSub3>();
		Iterator it = s.getBcStkTransfSubs().iterator();
		while (it.hasNext()) {
			BCStkTransfSub sub = (BCStkTransfSub) it.next();
			
			for (int i = 0; i < 2; i++) {
				BCStkTransfSub3 s3 = new BCStkTransfSub3();
				if(i==0) {
					s3.setMyType(12);					
					s3.setWhCode(sub.getToWH());
					s3.setShelfCode(sub.getToShelf());

					s3.setFromwhcode(sub.getFromWH());
					s3.setFromshelfcode(sub.getFromShelf());					
				} else {
					s3.setMyType(13);					
					s3.setWhCode(sub.getFromWH());
					s3.setShelfCode(sub.getFromShelf());

					s3.setFromwhcode(sub.getToWH());
					s3.setFromshelfcode(sub.getToShelf());						
				}

				s3.setDocNo(s.getDocNo());
				s3.setItemCode(sub.getItemCode());
				s3.setDocDate(s.getDocDate());
				s3.setDepartCode(s.getDepartCode());
				s3.setQty(sub.getQty());
				s3.setRefNo(sub.getRefNo());
				s3.setUnitCode(sub.getUnitCode());
				s3.setReftrlinenum(sub.getLineNumber());
				s3.setTaxRate(0.0);
				s3.setPackingRate1(1.0);
				s3.setPackingRate2(1.0);
				s3.setIsCancel(0);
				s3.setIsProcess(1);
				s3.setIsLockCost(0);
				s3.setIsExport(0);
				s3.setIsPOS(0);
				s3.setSumOfCost2(0.0);				
				
				al.add(s3);
			}	
		}
		
		return al;
	}

	@SuppressWarnings("unchecked")
	public BCStkTransfer setBean(ActionForm form, HttpServletRequest request) {

		String user 	 = (String)request.getSession().getAttribute("userName");

		BCStkTransfer2Form sf = (BCStkTransfer2Form)form;

		BCStkTransfer s = new BCStkTransfer();
		s.setDocNo(sf.getDocNo());
		s.setDocDate(CommonUtils.convertStringToDate(sf.getDocDate()));
		s.setDepartCode(sf.getDepartCode());
		try {
			s.setMyDescription(new String( sf.getMyDescription().getBytes("ISO8859_1"),"UTF-8" ));			
		}catch (Exception e) { }
		
		s.setIsConfirm(0);
		s.setIsCancel(0);
		s.setIsProcessOK(0);
		s.setIsCompleteSave(1);
		s.setCreatorCode(user);
		s.setCreateDateTime(new Date());
		
		double sumOfQty = 0;
		HashSet hs = new HashSet();
		for (int i = 0; i < sf.getItemCode().length; i++) {
			BCStkTransfSub sub = new BCStkTransfSub();
			sub.setRefNo(s.getDocNo());
			sub.setDocDate(s.getDocDate());
			sub.setDepartCode(s.getDepartCode());
			
			sub.setItemCode(sf.getItemCode()[i]);
			sub.setIttl_itemName(sf.getIttl_itemName()[i]);
			try {
				sub.setMyDescription(new String( sf.getMyDescriptionSub()[i].getBytes("ISO8859_1"),"UTF-8"));
			}catch (Exception e) {
				
			}
			sub.setFromWH(sf.getFromWH());
			sub.setFromShelf(sf.getFromShelf());
			sub.setToWH(sf.getToWH());
			sub.setToShelf(sf.getToShelf());
			sub.setQty(sf.getQty()[i]);
			sub.setUnitCode(sf.getUnitCode()[i]);
			
			sub.setTransState(0);
			sub.setIsCancel(0);
			sub.setLineNumber(i);
			
			sub.setPrice(0.0);
			sub.setSumOfCost(0.0);
			sub.setAmount(0.0);
			
			sub.setBcStkTransfer(s);
			hs.add(sub);
//			al.add(sub);
			
			sumOfQty +=sub.getQty();
		}
		s.setSumOfQty(sumOfQty);
		s.setBcStkTransfSubs(hs);
		return s;
	}

	public boolean cancel(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		
		
		return new BCStkTransferBO().view(form, request, response);
	}

	@SuppressWarnings("unchecked")
	private boolean view(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		String docNo = request.getParameter("docNo");
		String hql = "FROM BCStkTransfer WHERE docNo='"+docNo+"'";
		ArrayList<BCStkTransfer> al = (ArrayList<BCStkTransfer>)new BCStkTransferDAO().find(hql);
		if(al.size()>0) {
			
			BCStkTransfer s = al.get(0);
			
			request.setAttribute("docDate", (""+s.getDocDate()).substring(0, 10));
			request.setAttribute("s", s);
			request.setAttribute("sub", s.getBcStkTransfSubs());
			
			String fromShelf = "";
			String toShelf = "";
			Iterator it = s.getBcStkTransfSubs().iterator();
			while (it.hasNext()) {
				BCStkTransfSub sub = (BCStkTransfSub) it.next();
				fromShelf = sub.getFromShelf();
				toShelf=sub.getToShelf();
				
			}
			ArrayList<LabelValueBean> toShelfList = new BCStkTransfer2BO().setToShelfList(toShelf); 
			ArrayList<LabelValueBean> f = new BCStkTransfer2BO().setFromShelfList(toShelf);			
			ArrayList<LabelValueBean> fromShelfList = new ArrayList<LabelValueBean>(); 
			for (int i = 0; i < f.size(); i++) {
				LabelValueBean l = (LabelValueBean)f.get(i);
				if(l.getValue().equals(fromShelf)) {
					fromShelfList.add(l);
				}
			}
			
			request.setAttribute("countId", s.getBcStkTransfSubs().size());
			
			request.setAttribute("fromShelf", fromShelf);
			request.setAttribute("toShelf", toShelf);
			request.setAttribute("toShelfList", toShelfList);
			request.setAttribute("fromShelfList", fromShelfList);
			
			return true;
		}else {
			return false;
		}		
	}

	public boolean delete(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		
		BCStkTransfer s = setBean(form, request);
				
		String dept = request.getParameter("dept");
		request.setAttribute("message", "ยกเลิกข้อมูลใบโอนเรียบร้อย: "+s.getDocNo());
		request.setAttribute("dept", dept.trim());
		request.setAttribute("pageForward", "<meta http-equiv='refresh' content='1;" +
				"url=stktransfer.do?mode=showFind&dept="+dept+"&docNo="+s.getDocNo()+"'>");	
		request.setAttribute("itemFind", s.getDocNo());
		
		System.out.println("dept=|"+dept+"|");
		return 	new BCStkTransferDAO().delete(s);
	}
}
